package avicit.bdp.dgs.mdm.rest;

import avicit.bdp.dgs.mdm.dto.MdmRuleConfigDTO;
import avicit.bdp.dgs.mdm.utils.enums.MdmCodeTypeEnum;
import avicit.bdp.dgs.mdm.service.MdmRuleConfigService;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 主数据编码配置
 *
 * @author mayi
 */
@RestController
@RequestMapping("/api/bdp/dgs/mdm/rule/config/")
@Api(tags = "主数据编码配置")
public class MdmRuleConfigRest {

  @Resource private MdmRuleConfigService mdmRuleConfigService;

  @ApiOperation(value = "分页查询")
  @ApiImplicitParams({
    @ApiImplicitParam(value = "分类ID", name = "typeId", dataType = "String", paramType = "query"),
    @ApiImplicitParam(value = "模型ID", name = "modelId", dataType = "String", paramType = "query"),
    @ApiImplicitParam(
        value = "码段类型",
        name = "type",
        dataType = "String",
        paramType = "query",
        required = true,
        allowableValues = "GD,TZ,LS,RQ,YY,RL,TL,QL"),
    @ApiImplicitParam(value = "查询关键词", name = "keyWords", dataType = "String", paramType = "query"),
    @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int", paramType = "query"),
    @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int", paramType = "query")
  })
  @GetMapping
  public ResponseMsg<QueryRespBean<MdmRuleConfigDTO>> getPageList(
      @RequestParam(required = false) String typeId,
      @RequestParam(required = false) String modelId,
      @RequestParam String type,
      @RequestParam(required = false) String keyWords,
      @RequestParam(defaultValue = "1") Integer pageNo,
      @RequestParam(defaultValue = "10") Integer pageSize)
      throws Exception {

    ResponseMsg<QueryRespBean<MdmRuleConfigDTO>> responseMsg = new ResponseMsg<>();

    QueryRespBean<MdmRuleConfigDTO> queryRespBean =
        mdmRuleConfigService.getPageList(typeId, modelId, type, keyWords, pageNo, pageSize);

    responseMsg.setResponseBody(queryRespBean);
    return responseMsg;
  }

  @ApiOperation(value = "新增")
  @PostMapping
  public ResponseMsg<String> save(@RequestBody MdmRuleConfigDTO dto) {
    ResponseMsg<String> responseMsg = new ResponseMsg<>();
    mdmRuleConfigService.save(dto);
    responseMsg.setResponseBody(dto.getId());
    return responseMsg;
  }

  @ApiOperation(value = "修改")
  @PutMapping
  public ResponseMsg<Boolean> update(@RequestBody MdmRuleConfigDTO dto) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    mdmRuleConfigService.update(dto);
    responseMsg.setResponseBody(true);
    return responseMsg;
  }

  @ApiOperation(value = "批量删除")
  @ApiParam(value = "逗号分隔的id串", name = "ids", required = true)
  @DeleteMapping
  public ResponseMsg<Boolean> batchDelete(@RequestBody String ids) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(mdmRuleConfigService.batchDelete(ids));
    return responseMsg;
  }

  @ApiOperation(value = "通过ID查询")
  @ApiParam(value = "id", name = "id", required = true)
  @GetMapping("/{id}")
  public ResponseMsg<MdmRuleConfigDTO> queryById(@PathVariable String id) {
    ResponseMsg<MdmRuleConfigDTO> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(mdmRuleConfigService.queryById(id));
    return responseMsg;
  }

  @ApiOperation(value = "码段类型枚举")
  @GetMapping("/codeType")
  public ResponseMsg<Map<String, String>> codeType() {
    ResponseMsg<Map<String, String>> responseMsg = new ResponseMsg<>();
    Map<String, String> resultMap = new HashMap<>(16);
    MdmCodeTypeEnum[] values = MdmCodeTypeEnum.values();
    for (MdmCodeTypeEnum value : values) {
      resultMap.put(value.getCode(), value.getDesc());
    }
    responseMsg.setResponseBody(resultMap);
    return responseMsg;
  }

  /**
   * 列举编码规则
   * 注意：【模型管理】-模型配置-新增编码规则时引用
   * @return
   */
  @ApiOperation(value = "列举编码规则")
  @GetMapping("/list")
  public ResponseMsg<List<MdmRuleConfigDTO>> listRule() {
    ResponseMsg<List<MdmRuleConfigDTO>> responseMsg = new ResponseMsg<>();

    responseMsg.setResponseBody(mdmRuleConfigService.listRule());

    return responseMsg;
  }
}
